<?php

/**
 * 交易记录
 */

namespace app\member\common;

class FinanceCommon extends \app\member\common\MemberCommon {


    protected function index() {
        $this->setMeta('账户信息');
        $this->pageInfo['name'] = '账户信息';

        $model = target('member/PayLog');

        //当日消费
        $dayList = $model->loadList([
            '_sql' => 'A.time > ' . mktime(0, 0, 0, date("m"), date("d"), date("Y")) . ' AND A.time <' . mktime(23, 59, 59, date("m"), date("d"), date("Y")),
        ]);

        $dayCount = [];
        $daySpend = 0;
        $dayCharge = 0;
        foreach ($dayList as $vo) {
            if ($vo['type']) {
                $dayCharge += $vo['money'];
            } else {
                $daySpend += $vo['money'];
            }
        }
        $dayCountNum = $daySpend + $dayCharge;
        $daySpendRate = $dayCountNum ? round(($daySpend / $dayCountNum) * 100) : 50;
        $dayChargeRate = $dayCountNum ? round(($dayCharge / $dayCountNum) * 100) : 50;
        $dayCount['series'][] = $daySpendRate;
        $dayCount['series'][] = $dayChargeRate;

        //本月消费
        $monthList = $model->loadList([
            '_sql' => 'A.time > ' . mktime(0, 0, 0, date("m", time()), 1, date("Y", time())) . ' AND A.time <' . mktime(23, 59, 59, date("m", time()), date('t'), date("Y", time())),
        ]);
        $monthCount = [];
        $monthSpend = 0;
        $monthCharge = 0;
        foreach ($monthList as $vo) {
            if ($vo['type']) {
                $monthCharge += $vo['money'];
            } else {
                $monthSpend += $vo['money'];
            }
        }

        $monthCountNum = $monthSpend + $monthCharge;

        $monthSpendRate = $monthCountNum ? round(($monthSpend / $monthCountNum) * 100) : 50;
        $monthChargeRate = $monthCountNum ? round(($monthCharge / $monthCountNum) * 100) : 50;

        $monthCount['series'][] = $monthSpendRate;
        $monthCount['series'][] = $monthChargeRate;


        //本年消费
        $yearList = $model->loadList([
            '_sql' => 'A.time > ' . strtotime(date("Y", time()) . "-1" . "-1") . ' AND A.time <' . strtotime(date("Y", time()) . "-12" . "-31"),

        ]);
        $yearCount = [];
        $yearCount['labels'] = [];
        $yearCount['series'] = [];
        for ($i = 1; $i < 13; $i++) {
            $yearCount['labels'][] = $i . '月';
        }

        $yearSpend = [];
        $yearCharge = [];
        foreach ($yearList as $vo) {
            if ($vo['type']) {
                $yearCharge[intval(date('m', $vo['time']))] += $vo['money'];
            } else {
                $yearSpend[intval(date('m', $vo['time']))] += $vo['money'];
            }
        }
        $i = 1;
        $yearSpendData = [];
        $yearChargeData = [];

        for ($i = 1; $i < 13; $i++) {
            $yearSpendData[] = $yearSpend[$i] ? $yearSpend[$i] : '0';
            $yearChargeData[] = $yearCharge[$i] ? $yearCharge[$i] : '0';
        }
        $yearCount['series'][] = $yearSpendData;
        $yearCount['series'][] = $yearChargeData;


        $this->assign('yearCountJson', json_encode($yearCount));
        $this->assign('dayCountJson', json_encode($dayCount));
        $this->assign('monthCountJson', json_encode($monthCount));

        $this->assign('dayInfo', [
            'spend' => $daySpend,
            'charge' => $dayCharge
        ]);
        $this->assign('monthInfo', [
            'spend' => $monthSpend,
            'charge' => $monthCharge
        ]);
        $this->memberDisplay();
    }

    protected function log() {
        $this->setMeta('收支明细');
        $this->pageInfo['name'] = '收支明细';

        $urlParam = [];
        $where = [];
        $type = request('get', 'type', 0, 'intval');

        $urlParam['type'] = $type;
        if ($type == 1) {
            $where['A.type'] = 1;
        }
        if ($type == 2) {
            $where['A.type'] = 0;
        }

        $pageLimit = 10;
        $where['A.account_id'] = $this->userInfo['finance_account_id'];

        $model = target('member/PayLog');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'log_id desc');

        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->assign('pageParams', $urlParam);
        $this->assign('type', $type);
        $this->memberDisplay();
    }

}